<template>
  <div class="app-main-container">
    <QueryForm
      v-model="filters"
      :query-form-config="queryFormConfig"
      @handleQuery="getList(1)"
      @clear="clearList"
    />
    <el-table
      ref="table"
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      :header-row-class-name="'table-header'"
      class="doumee-element-tableb"
    >
      <el-table-column
        align="center"
        :label="$t('message.ddbh')"
        min-width="220"
        show-overflow-tooltip
        prop="orderId"
      />
      <el-table-column
        align="center"
        :label="$t('message.yhxx')"
        min-width="200"
        prop="memberInfo"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="$t('message.kcmc')"
        min-width="200"
        prop="courseName"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="$t('message.kcjgy')"
        prop="allPrice"
        min-width="110"
      />
      <el-table-column
        align="center"
        :label="$t('message.zffs')"
        prop="payMethod"
        show-overflow-tooltip
        min-width="120"
      >
        <template slot-scope="scope">
          {{
            nameWithPayMethod(scope.row.payMethod)
          }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="$t('message.createTime')"
        prop="createTime"
        min-width="160"
      />
      <el-table-column
        align="center"
        :label="$t('message.paytime')"
        prop="payTime"
        min-width="160"
      />
      <el-table-column
        align="center"
        :label="$t('message.bz')"
        prop="note"
        min-width="120"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="$t('message.ddzt')"
        prop="orderStatus"
        min-width="100"
        fixed="right"
      >
        <template slot-scope="scope">
          {{
            scope.row.orderStatus == '0' ? $t('message.dzf')
            :scope.row.orderStatus == '1' ? $t('message.cswzf')
              :scope.row.orderStatus == '2' ? $t('message.qxzf')
                :scope.row.orderStatus == '3' ? $t('message.zfcg')
                  :scope.row.orderStatus == '4' ? $t('message.tkz')
                    :scope.row.orderStatus == '5' ? $t('message.tkcg') : ''
          }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('message.caozuo')" min-width="100" align="center" fixed="right">
        <template slot-scope="scope">
          <div class="flex-cc">
            <el-button
              v-if="scope.row.orderStatus == 3 && meta.indexOf('OfflinecoursesOrderRefund') > -1"
              v-preventReClick
              type="text"
              @click="tk(scope.row)"
            >{{ $t('message.tk') }}</el-button>
            <span v-else>-</span>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <Paginations
      :total="total"
      :page="pagination.page"
      @current-change="getList"
      @SizeChange="handleSizeChange"
    />
  </div>
</template>

<script>
import { parseTime } from '@/utils/index'
import { courseOrderVenueList, courseRefund, allConfigList } from '@/api'
import { queryFormConfig } from './config'
import QueryForm from '@/components/QueryForm'
export default {
  name: 'OnlineCoursesOrder',
  components: {
    QueryForm
  },
  data() {
    return {
      queryFormConfig,
      meta: [],
      list: [],
      pagination: {
        page: 1,
        rows: 10
      },
      total: 0,
      filters: {},
      allPayMethedList: [],
      listLoading: true
    }
  },
  created() {
    this.meta = this.$route.meta.buttons || []
    this.pdgj()
    this.getList(1)
  },
  activated() {
    this.$refs.table.doLayout()
  },
  methods: {
    handleSizeChange(value) {
      this.pagination.rows = value
      this.getList(1)
    },
    pdgj() {
      allConfigList({ param: { type: '2', code: '1' }}).then(res => {
        if (res.errorCode === '000000') {
          this.allPayMethedList = res.recordList
          const arrb = res.recordList
          arrb.forEach(e => {
            e.label = e.name
          })
          this.queryFormConfig.formItems[2].options = arrb
        }
      }).catch(err => {
        console.log(err)
      })
    },
    nameWithPayMethod(payM) {
      const temp = this.allPayMethedList.find((item) => item.value === payM)
      return temp ? temp.name : '-'
    },
    tk(e) {
      const that = this
      this.$confirm(this.$t('message.tktc'), this.$t('message.ts'), {
        confirmButtonText: this.$t('message.qd'),
        cancelButtonText: this.$t('message.qx'),
        type: 'warning'
      }).then(() => {
        courseRefund({ param: { orderId: e.orderId }}).then(res => {
          if (res.errorCode === '000000') {
            that.$message.success(this.$t('message.tktcqr'))
            that.getList(1)
          }
        }).catch(() => {})
      })
    },
    clearList() {
      this.filters = {}
      this.getList(1)
    },
    getList(page) {
      this.listLoading = true
      const { filters, pagination } = this
      const venueId = sessionStorage.getItem('venueId')
      filters.flag = '1'
      if (page) {
        pagination.page = page
        this.pagination.page = page
      }
      pagination.firstQueryTime = parseTime(new Date())
      courseOrderVenueList({ pagination, param: { venueId, ...filters }})
        .then(res => {
          this.listLoading = false
          if (res.errorCode === '000000') {
            this.list = res.recordList
            this.total = res.totalCount
          }
        })
        .catch(() => {
          this.listLoading = false
        })
    }
  }
}
</script>
